<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="profileObj.photo" @click="imageClickFn"/>
           <!-- file 选择框 -->
           <input
                  type="file"
                  ref="iptFile"
                  v-show="false"
                  accept="image/*"
                  @change="onFileChange"
                  />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profileObj.name" @click="showNameDialogFn"/>
      <van-cell title="生日" is-link :value="profileObj.birthday" @click="showDateFn"/>
    </van-cell-group>
    <!-- 修改用户名称的弹框 -->
    <van-dialog v-model="show" title="标题" show-cancel-button :before-close="beforeCloseFn">
        <input type="text" v-zdybq v-model="inputUserName">
    </van-dialog>
    <!-- 时间选择器-生日日期组件 -->
    <van-popup v-model="dateShow" position="bottom" :style="{ height: '50%' }" round>
        <van-datetime-picker v-model="currentDate" type="date" title="选择出生日期"
        :min-date="minDate" :max-date="maxDate" @cancel="cancelDateFn" @confirm="confirmDateFn"/>
    </van-popup>

  </div>
</template>

<script>
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api'
import Notify from '@/ui/Notify'
import { formatDate } from '@/utils/date'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {},
      show: false,
      inputUserName: '', // 保存修改姓名弹窗里面的值
      minDate: new Date(1920, 0, 1), //
      maxDate: new Date(), //
      currentDate: new Date(),
      dateShow: false
    }
  },
  async created () {
    const res = await userProfileAPI()
    console.log(res)
    this.profileObj = res.data.data
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO', 'SET_USERNAME']),
    // 文件选择改变 - 事件
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      console.log(e.target.files[0])
      //   创建formData对象
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])// 请求体里加入一对参数名和值
      const res = await updateUserPhotoAPI(theFd)
      console.log(res)
      this.profileObj.photo = res.data.data.photo
      this.SET_USERPHOTO(res.data.data.photo)// 更新成功后，同步到Vuex
    },
    // 图片 - 点击事件
    imageClickFn () {
      this.$refs.iptFile.click()// js模拟标签的事件触发
    },
    // 修改姓名弹框
    showNameDialogFn () {
      this.show = true
      this.inputUserName = this.profileObj.name
    },
    // 确认关闭姓名弹框方法
    async beforeCloseFn (action, done) {
    //   console.log(action, done)
      if (action === 'confirm') {
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName) === true) {
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          this.profileObj.name = this.inputUserName
          this.SET_USERNAME(this.inputUserName)// 更新名字成功后，同步到Vuex
          done()// 通过校验
        } else { // 没通过校验，给用户提示
          Notify({ type: 'warning', message: '用户名只能是1-7位中英数字组合' })
          done(false)
        }
      } else {
        done()
      }
    },
    // 展示生日日期
    showDateFn () {
      this.dateShow = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 日期选择器 - 取消事件
    cancelDateFn () {
      this.dateShow = false
    },
    // 日期选择器 - 确认
    async confirmDateFn () {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.profileObj.birthday = formatDate(this.currentDate)
      this.dateShow = false
    }
  }
}
</script>

  <style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
.van-dialog{
    text-align: center;
    input{
        border: none;
        text-align: center;
        outline: none;
        padding: 0;
    }
}
</style>
